package org.budo.asr.service.aliyun;

import java.util.ArrayList;
import java.util.List;

import org.budo.asr.service.api.DefaultAsrResponse;
import org.budo.support.fastjson.Fastjson;
import org.budo.support.lang.util.StringUtil;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import lombok.extern.slf4j.Slf4j;

/**
 * @author lmw
 */
@Slf4j
public class AliyunAsrResponse extends DefaultAsrResponse {
    @Override
    public String getText() {
        if (null == this.getResponseBody()) {
            log.info("#15 no ResponseBody this=" + this);
            return "";
        }

        JSONObject jsonObject = Fastjson.parseObject(this.getResponseBody());
        JSONArray sentences = jsonObject.getJSONArray("Sentences");
        if (null == sentences || sentences.isEmpty()) {
            log.info("#15 no sentences this=" + this);
            return "";
        }

        List<String> textList = new ArrayList<String>();
        for (Object each : sentences) {
            JSONObject sentence = (JSONObject) each;
            String text = sentence.getString("Text");

            textList.add(text);
        }

        return StringUtil.join(textList, "；");
    }

    @Override
    public String toString() {
        return this.getClass().getName() + "@" + Integer.toHexString(this.hashCode()) //
                + ", text=" + this.getText() + ", super=" + super.toString();
    }
}